var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    branchList:[],
    markers:[],
    latitude: 0,
    longitude: 0,
    CurrentPage:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLocationInfo();
  },
  
  //获取当前位置
  getLocationInfo:function(){
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = parseFloat(res.latitude) 
        var longitude = parseFloat(res.longitude)
        that.setData({
          latitude: latitude,
          longitude: longitude
        })
        that.getBranchList(latitude, longitude);
        that.getRegionCity(latitude, longitude)
      }
    })
  },

  //获取门店列表
  getBranchList: function (latitude, longitude){
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Store/GetBranchList",
      data: {
        storeId:app.globalData.storeId,
        pageNumber: that.data.CurrentPage,
        pageSize:8,
        latitude: latitude,//当前纬度
        longitude: longitude//当前经度
      },
      success: function(res) {
       let markers = [];
       if(res.data.Code == 0){
         var markersList = res.data.Data.List;
         for (var i = 0; i < markersList.length;i++){
            let h = {}
            h.latitude = markersList[i].latitude
            h.longitude = markersList[i].longitude
            h.id = i
            h.iconPath = "/images/nav/map.png"
            h.width = 15
            h.height = 30
            markers.push(h)
         }
         if (that.data.CurrentPage != 1) {
           var preGoods = that.data.branchList
           markersList = preGoods.concat(markersList);
         }
         that.setData({
           branchList: markersList,
           markers: markers,
           loadingMoreHidden:res.data.Data.PageModel.HasNextPage
         })
       }
      },
    })
  },

  //加载更多
  loadMore: function (e) {
    var that = this;
    if (!that.data.loadingMoreHidden) return;
    var currPage = that.data.CurrentPage;
    that.setData({
      CurrentPage: currPage + 1
    })
      that.getBranchList(that.data.latitude, that.data.longitude);
  },
  
  //跳传详情页面
  toStoreDetails:function(e){
    wx.navigateTo({
      url: '/pages/storeDetails/index?bid=' + e.currentTarget.dataset.bid,
    })
  },

  //获取坐标对应的地址信息
  getRegionCity: function (latitude, longitude){
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Common/GetRegionByGeo?lat=" + latitude + "&lng=" + longitude,
      success: function(res) {
        if(res.data.Code == 0){
          wx.setNavigationBarTitle({
            title: res.data.Data.CityName
          })
        }
      },
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  }
})